package com.tarena.lbs.common.basic.converter;

import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.dos.AdminDO;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

@Configuration
public class AdminConverter {

    public AdminVO bo2vo(AdminBO bo){
        if (null == bo){
            return null;
        }
        AdminVO adminVO = new AdminVO();
        BeanUtils.copyProperties(bo,adminVO);
        return adminVO;
    }

    public AdminDO param2DO(AdminParam param){
        if (null == param){
            return null;
        }
        AdminDO adminDO = new AdminDO();
        BeanUtils.copyProperties(param,adminDO);
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            adminDO.setBusinessId(loginUser.getId().intValue());
        }
        return adminDO;
    }

    public AdminBO param2BO(AdminParam param){
        if (null == param){
            return null;
        }
        AdminBO adminBO = new AdminBO();
        BeanUtils.copyProperties(param,adminBO);
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            adminBO.setBusinessId(loginUser.getId().intValue());
        }
        return adminBO;
    }

    public AdminBO do2bo(AdminDO AdminDO) {
        if (null == AdminDO){
            return null;
        }
        AdminBO AdminBO = new AdminBO();
        BeanUtils.copyProperties(AdminDO,AdminBO);
        return AdminBO;
    }


    public List<AdminBO> doList2boList(List<AdminDO> doList ){
        List<AdminBO> boList = new ArrayList<>();
        if (CollectionUtils.isEmpty(doList)) {
            return boList;
        }
        for (AdminDO AdminDO : doList) {
            AdminBO AdminBO = new AdminBO();
            BeanUtils.copyProperties(AdminDO,AdminBO);
            boList.add(AdminBO);
        }
        return boList;
    }

    public List<AdminVO> boList2VoList(List<AdminBO> boList ){
        List<AdminVO> AdminVoList = new ArrayList<>();
        if (CollectionUtils.isEmpty(boList)) {
            return AdminVoList;
        }
        for (AdminBO adminBO : boList) {
            AdminVO adminVO = new AdminVO();
            BeanUtils.copyProperties(adminBO,adminVO);
            AdminVoList.add(adminVO);
        }
        return AdminVoList;
    }

    public PageResult<AdminVO> assemblePageVOS(BasePageList<AdminBO> bos, AdminQuery query) {
        if (bos == null || bos.getTotal() == 0) {
            return null;
        }
        PageResult<AdminVO> result = new PageResult<>();
        List<AdminVO> vos = new ArrayList<>();
        for (AdminBO temp : bos.getList()) {
            AdminVO vo = new AdminVO();
            BeanUtils.copyProperties(temp,vo);
            vos.add(vo);
        }
        result.setTotal(bos.getTotal());
        result.setObjects(vos);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());
        return result;
    }
}
